package com.genghis.ptas.classify;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.GeneralSecurityException;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("classify/")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "listAllType", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllTypeDo() {
        return "backstage/classify/listAllType";
    }

    @RequestMapping(value = "listAllTypeDo", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllType(PageParameter pageParameter, Type type) {
        return classifyService.findTypePageList(pageParameter, type);
    }

    @RequestMapping(value = "addTypeDo", method = {RequestMethod.GET})
    public String addTypeDo() {
        return "backstage/classify/addTypeDo";
    }

    @RequestMapping(value = "checkRepeatTypeName", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkRepeatTypeName(Type type) {
        return classifyService.checkRepeatTypeName(type);
    }

    @RequestMapping(value = "addType", method = {RequestMethod.POST})
    @ResponseBody
    public void addType(Type type) {
        classifyService.addType(type);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "editTypeDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editTypeDo(HttpServletRequest request) throws GeneralSecurityException {
        Type type = classifyService.findTypeByTypeName(StringUtil.iso2utf(request.getParameter("typeName")));
        request.setAttribute("type", type);
        return "backstage/classify/editTypeDo";
    }

    @RequestMapping(value = "editType", method = RequestMethod.POST)
    @ResponseBody
    public void editType(Type type) throws GeneralSecurityException {
        classifyService.editType(type);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "delType", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delType(@RequestParam("typeName") String typeNames) throws GeneralSecurityException {
        classifyService.delType(StringUtil.iso2utf(typeNames));
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "listAllTypeValuesDo", method = RequestMethod.GET)
    public String listAllTypeValuesDo(HttpServletRequest request) {
        request.setAttribute("typeName", StringUtil.iso2utf(request.getParameter("typeName")));
        return "backstage/classify/listAllTypeValues";
    }

    @RequestMapping(value = "listAllTypeValues", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllTypeValues(PageParameter pageParameter, TypeValues typeValue) {
        return classifyService.findTypeValuePageList(pageParameter, typeValue);
    }

    @RequestMapping(value = "checkRepeatTypeValue", method = {RequestMethod.POST})
    @ResponseBody
    public boolean checkRepeatTypeValuesCode(TypeValues typeValue) {
        return classifyService.checkRepeatTypeValue(typeValue);
    }

    @RequestMapping(value = "addTypeValuesDo", method = {RequestMethod.GET})
    public String addTypeValuesDo(HttpServletRequest request) {
        request.setAttribute("typeName", StringUtil.iso2utf(request.getParameter("typeName")));
        return "backstage/classify/addTypeValueDo";
    }

    @RequestMapping(value = "addTypeValue", method = {RequestMethod.POST})
    @ResponseBody
    public void addType(TypeValues typeValue) {
        classifyService.addTypeValue(typeValue);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "editTypeValuesDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editTypeValuesDo(HttpServletRequest request, TypeValues typeValue) throws GeneralSecurityException {
        typeValue.setTypeName(StringUtil.iso2utf(typeValue.getTypeName()));
        typeValue.setTypeValue(StringUtil.iso2utf(typeValue.getTypeValue()));
        request.setAttribute("typeValue", typeValue);
        return "backstage/classify/editTypeValueDo";
    }

    @RequestMapping(value = "editTypeValue", method = RequestMethod.POST)
    @ResponseBody
    public void editTypeValue(TypeValues typeValues) throws GeneralSecurityException {
        classifyService.editTypeValues(typeValues);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "delTypeValue", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delTypeValue(TypeValues typeValues) throws GeneralSecurityException {
        classifyService.delTypeValue(typeValues);
        autoCompleteService.initAutoComplete();
    }

}
